#include "processor.h"

Processor::Processor(RunMode mode)
{
	m_mode = mode;
	m_stoping = false;
}

Processor::~Processor()
{
}

bool Processor::Run()
{
	m_stoping = false;
	
	OnRun();
	
	switch(m_mode)
	{
		case rm_Thread:
			pthread_create( &th, NULL, (void* (*)(void*))Processor::Work, this);
			break;
		case rm_Process:
			int pid = fork();
			if(pid == 0)
				Work(this);
			break;
	}
	
	OnAfterRun();
	return true;
}

void Processor::Stop()
{
	OnStop();
	m_stoping = true;
	OnAfterStop();
}

RunMode Processor::GetMode()
{
	return m_mode;
}

void Processor::Work(Processor * object)
{
	while(!(object->m_stoping))
	{
		object->Process();		
	}
	
	if(object->m_mode == rm_Process)
		exit(0);
}